﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Biblioteca.DataBase;
using Biblioteca.Utils;
using SEG.Utils;
using Pms.Utils;

namespace SEG.Objetos
{
    public class Usuario_Empresa
    {


        private string cod_empresa;              
        private string cod_usu;       
        private string padrao;


        public Usuario_Empresa()
        { }

        public Usuario_Empresa(string cod_empresa, string cod_usu)
        {
            this.cod_empresa = cod_empresa;
            this.cod_usu = cod_usu;
        }




        public string Cod_empresa
        {
            get { return cod_empresa; }
            set { cod_empresa = value; }
        }

        public string Cod_usu
        {
            get { return cod_usu; }
            set { cod_usu = value; }
        }

        public string Padrao
        {
            get { return padrao; }
            set { padrao = value; }
        }


        public static void Inserir(string loginAut, List<Usuario_Empresa> listUsuEmp)
        {
            OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            try
            {

                string sql = @"insert into seg_usuario_empresa values (
                               :cod_usu,:cod_empresa,:padrao)";


                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSEG, loginAut);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandText = sql;


                foreach (Usuario_Empresa objUsuEmp in listUsuEmp)
                {
                    command.Parameters.Add(new OracleParameter("COD_USU", OracleType.Number));
                    command.Parameters.Add(new OracleParameter("COD_EMPRESA", OracleType.Number));
                    command.Parameters.Add(new OracleParameter("PADRAO", OracleType.VarChar));

                    command.Parameters["COD_USU"].Value = objUsuEmp.cod_usu;
                    command.Parameters["COD_EMPRESA"].Value = objUsuEmp.cod_empresa;                    
                    command.Parameters["PADRAO"].Value = objUsuEmp.padrao;

                    command.ExecuteNonQuery();

                    command.Parameters.Clear();

                }

                command.Transaction.Commit();

            }
            catch (Exception)
            {

                command.Transaction.Rollback();
                throw;
            }
            finally
            {
                command.Connection.Close();
            }

        }

        public static void Excluir(string loginAut, List<Usuario_Empresa> listUsuEmp)
        {
            OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            try
            {

                string sql = @"delete from seg_usuario_empresa where cod_usu =:cod_usu
                                and cod_empresa = :cod_empresa";


                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSEG, loginAut);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandText = sql;


                foreach (Usuario_Empresa objUsuEmp in listUsuEmp)
                {
                    command.Parameters.Add(new OracleParameter("COD_USU", OracleType.Number));
                    command.Parameters.Add(new OracleParameter("COD_EMPRESA", OracleType.Number));
                    

                    command.Parameters["COD_USU"].Value = objUsuEmp.cod_usu;
                    command.Parameters["COD_EMPRESA"].Value = objUsuEmp.cod_empresa;

                    command.ExecuteNonQuery();

                    command.Parameters.Clear();

                }

                command.Transaction.Commit();

            }
            catch (Exception)
            {

                command.Transaction.Rollback();
                throw;
            }
            finally
            {
                command.Connection.Close();
            }

        }

    }
}
